/*
 * 文件名：PushParkLot.java	 
 * 时     间：下午7:11:21
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.manager.thread;

import java.util.Date;

import com.local.common.PayTypeConstans;
import com.local.dao.hismappers.LocalPayBillMapper;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.model.CarParkOut;
import com.local.model.Integration;
import com.local.model.LocalCoupon;
import com.local.model.LocalPayBill;
import com.local.service.face.CrossingService;
import com.local.service.face.IntegrationService;
import com.local.service.face.LocalCouponService;
import com.local.util.MyDateUtil;
import com.qianhai.common.entity.LoginUserInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.utils.Log4jUtil;
import com.qianhai.common.utils.ShiroUtil;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:推送车位信息,车位总数减去在场车辆
 * 
 * @ClassName: PushParkLot
 * @author Administrator
 * @date 2015年9月29日 下午7:11:21
 */
public class CreatePayBillThread implements Runnable
{
	private static Log4jUtil log = Log4jUtil.getLog4j(CreatePayBillThread.class);
	
	private ParkwellPayBillVo parkwellPayBillVo;
	
	private CarParkOut carParkOut;
	
	private CrossingService crossinService;
	
	private LocalCouponService localCouponService;
	
	private IntegrationService integrationService;
	private LocalPayBillMapper parkwellPayBillMapper;
	
	
	public CreatePayBillThread(ParkwellPayBillVo parkwellPayBillVo,CarParkOut carParkOut,CrossingService crossinService,LocalCouponService localCouponService,IntegrationService integrationService,LocalPayBillMapper parkwellPayBillMapper)
	{
		super();
		this.parkwellPayBillVo = parkwellPayBillVo;
		this.carParkOut = carParkOut;
		this.crossinService = crossinService;
		this.localCouponService = localCouponService;
		this.integrationService = integrationService;
		this.parkwellPayBillMapper = parkwellPayBillMapper;
	}



	@Override
	public void run()
	{
		
		try
		{
			/*// 判断相机传送过来的数据是否有路口信息，防止伪数据
			List<Crossing> crossList = crossinService
					.queryCrossingByEquimentNo(carParkOut.getEquipment());
			
			if (Tools.isEmpty(crossList) || crossList.isEmpty())
			{
				parkwellPayBillVo.setError(true);
				parkwellPayBillVo.setErrorMsg("出场闸机或相机故障，请先排除设备故障.");
				parkwellPayBillVo.setResult(false);
				return 1;
			}*/
			// 更新进出场纪录
			carParkOut.setCarInUuid(parkwellPayBillVo.getCarInUuid());
			/*carInAndOutService.addCarParkOutBill(carParkOut);*/
			
			// 打印流水
			LocalPayBill parkwellPayBill = new LocalPayBill();
			parkwellPayBill.setParkInUuid(parkwellPayBillVo.getCarInUuid());
			parkwellPayBill.setPayBillUuid(UUIDGenerateUtil.generate());
			parkwellPayBill.setCarNo(parkwellPayBillVo.getCarNo());
			parkwellPayBill.setPayTime(new Date(System.currentTimeMillis()));
			
			LoginUserInfo loginUser = ShiroUtil.getCurrentUser();
			if (!Tools.isEmpty(loginUser))
			{
				parkwellPayBill.setUserId(loginUser.getUuid());
				parkwellPayBill.setUserName(loginUser.getLoginName());
			}
			parkwellPayBill.setPrice(!Tools.isEmpty(parkwellPayBillVo
					.getTheoryPriceDb()) ? parkwellPayBillVo.getTheoryPriceDb()
					: 0);
			
			if (parkwellPayBillVo.getCarNoTye().equals(
					PayTypeConstans.MENMBER_TYPE)
					|| parkwellPayBillVo.getCarNoTye().equals("白名单"))
			{
				parkwellPayBill.setActualPrice(0d);
			} else
			{
				
				parkwellPayBill.setActualPrice(!Tools.isEmpty(parkwellPayBillVo
						.getPriceDifference()) ? parkwellPayBillVo
						.getPriceDifference() : 0);
			}
			
			// 如果没有指定支付类型则为自动开闸出场车辆,否则为收现金执行开闸
			if (Tools.isEmpty(parkwellPayBillVo.getPayType()))
			{
				parkwellPayBill.setPayType(parkwellPayBillVo.getCarNoTye());
			} else
			{
				parkwellPayBill.setPayType(parkwellPayBillVo.getPayType());
			}
			
			// 处理纸质优惠券
			if (!Tools.isEmpty(parkwellPayBillVo.getCoupon())
					&& parkwellPayBillVo.getCoupon() > 0)
			{
				LocalCoupon lcoupon = new LocalCoupon();
				lcoupon.setBillUuid(parkwellPayBill.getPayBillUuid());
				lcoupon.setCarNo(parkwellPayBillVo.getCarNo());
				lcoupon.setCouponPrice(parkwellPayBillVo.getCoupon()
						.doubleValue());
				lcoupon.setCouponUuid(UUIDGenerateUtil.generate());
				lcoupon.setPayTime(new Date(System.currentTimeMillis()));
				lcoupon.setUserId(ShiroUtil.getCurrentUser().getUuid());
				lcoupon.setUserName(ShiroUtil.getCurrentUser().getLoginName());
				localCouponService.insertSelective(lcoupon);
				
				// 2015-11-20 防止线程死锁。
				parkwellPayBillVo.setLcoupon(lcoupon);
				
				// 流水实际支付减去纸质优惠券金额
				Double payNmub = parkwellPayBillVo.getPriceDifference();
				if (!Tools.isEmpty(payNmub)
						&& payNmub - parkwellPayBillVo.getCoupon() > 0)
				{
					parkwellPayBill.setActualPrice(payNmub
							- parkwellPayBillVo.getCoupon());
				}
			}
			
			if (Tools.isEmpty(parkwellPayBill.getActualPrice())
					|| parkwellPayBill.getActualPrice() < 0)
			{
				parkwellPayBill.setActualPrice(0d);
			}
			
			// 如是自动扣费
			if (parkwellPayBillVo.isAuotPayOut())
			{
				Integration ingra = integrationService
						.queryInterationByCarNo(parkwellPayBillVo.getCarNo());
				ingra.setLessPrice(ingra.getLessPrice()
						- parkwellPayBillVo.getPriceDifference());
				integrationService.updateByPrimaryKeySelective(ingra);
				parkwellPayBill.setActualPrice(parkwellPayBillVo
						.getPriceDifference());
			}
			
			// 2015-12-10 添加进场出场信息
			parkwellPayBill.setCarOutUuid(carParkOut.getUuid());
			parkwellPayBill.setCaroutCareteAt(carParkOut.getCreateTime());
			parkwellPayBill.setCarinCreatAt(parkwellPayBillVo.getCarInTime());
			if (!Tools.isEmpty(parkwellPayBill.getCarinCreatAt())
					&& !Tools.isEmpty(parkwellPayBill.getCaroutCareteAt()))
			{
				// 停车时长
				String stayTimeLong = MyDateUtil.betweenTimeString(
						parkwellPayBill.getCaroutCareteAt(),
						parkwellPayBill.getCarinCreatAt());
				parkwellPayBill.setStayTimeLong(stayTimeLong);
			}
			// 2015-12-10 end
			
			parkwellPayBillMapper.insertSelective(parkwellPayBill);
			
			parkwellPayBillVo.setPayBill(parkwellPayBill);
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new BaseException(e.getMessage());
		}
	}
}
